﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.eCard.Agent
{
    public partial class SaleAgentProdAdd : System.Web.UI.Page
    {
        string iID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] != null)
            {
                iID = Request["id"].ToString();
            }
            if (!Page.IsPostBack)
            {
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                drop_saleProduct.DataSource = dsProd;
                drop_saleProduct.DataTextField = "procduct_name";
                drop_saleProduct.DataValueField = "procduct_code";
                drop_saleProduct.DataBind();
                ListItem liAll = new ListItem();                
                drop_saleProduct.Items.Insert(0, liAll);

                DataSet dsAgent = SaleAgentDao.getSaleAgentList("Y");
                drop_saleAgent.DataSource = dsAgent;
                drop_saleAgent.DataTextField = "sale_agent_name";
                drop_saleAgent.DataValueField = "sale_agent_id";
                drop_saleAgent.DataBind();

                //Comm.BindDll(ref dropPeriodType, "TermUnit", false, "");
                if (!iID.Equals(""))
                {
                    bindPage();
                    btnSave.Enabled = false;
                }
                else
                {
                    btnEdit.Enabled = false;
                }
            }
        }

        private void bindPage()
        {
            try
            {
                DataSet ds = SaleAgentDao.getAgentProdbyID(int.Parse(iID));                
                drop_saleAgent.Items.FindByValue(ds.Tables[0].Rows[0]["sale_agent_id"].ToString().Trim()).Selected = true;
                drop_saleProduct.Items.FindByValue(ds.Tables[0].Rows[0]["prod_code"].ToString().Trim()).Selected = true;
                txtAgentDiscount.Value = ds.Tables[0].Rows[0]["agent_discount"].ToString().Trim();

            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("EntityAdd_bindPage", ex.ToString());
                Response.Write(ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string sale_agent_id = drop_saleAgent.SelectedItem.Value;
                string sale_prod_id = drop_saleProduct.SelectedItem.Value;
                string agent_discount = txtAgentDiscount.Value.Trim();

                bool bok = SaleAgentDao.AgentProdAdd(int.Parse(sale_agent_id),sale_prod_id,float.Parse(agent_discount));
                if (bok)
                    KVB.Common.JavaScript.ViewMsgGoUrl("SaleAgentProd.aspx", "添加成功");
                else
                    Response.Write("<script language=javascript>alert('添加失败');</script>");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("SaleAgentProdAdd.btnSave_Click", ex.ToString());
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SaleAgentProd.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string sale_agent_id = drop_saleAgent.SelectedItem.Value;
                string sale_prod_id = drop_saleProduct.SelectedItem.Value;
                string agent_discount = txtAgentDiscount.Value.Trim();

                bool bok = SaleAgentDao.AgentProdUpdate(int.Parse(iID), int.Parse(sale_agent_id), sale_prod_id, float.Parse(agent_discount));

                if (bok)
                    Response.Write("<script language=javascript>alert('修改成功');</script>");
                else
                    Response.Write("<script language=javascript>alert('修改失败');</script>");
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("SaleAgentProdAdd.btnEdit_Click", ex.ToString());
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }
    }
}